﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Entities;
using OreSystem.BLL;
using OreSystem.Common;
namespace MainFrom
{
    public partial class CartEdit : Form
    {
        public IList<Level> Plevels { get; set; }
        public Car NewCar { get; set; }
        public CarBLL carBLL = new CarBLL();
        private LevelBll levelBll = new LevelBll();
        //private bool flag = true;
        public CartEdit()
        {
            InitializeComponent();
        }

        private void CarEdit_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            lbl_message.ForeColor = Color.Red;
            cmb_Pid.DisplayMember = "Name";
            cmb_Pid.ValueMember = "ID";
            cmb_Pid.DataSource = Plevels;
            
            if (NewCar != null)
            {
               // flag = false;
                //cmb_Pid.SelectedItem = levelBll.GetLevelById(NewCar.PID);
                cmb_Pid.SelectedValue = NewCar.PID;
                txt_CarNo.Text = NewCar.CarNo;
                txt_Cubic.Text = NewCar.Cubic.ToString();
                txt_License.Text = NewCar.LicensePlate;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmb_Pid.SelectedItem == null) {
                //MessageBox.Show("车编号不能为空！");
                lbl_message.Text = "车的上层不能为空";
                cmb_Pid.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_CarNo.Text))
            {
                //MessageBox.Show("车编号不能为空！");
                lbl_message.Text = "车编号不能为空";
                txt_CarNo.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_Cubic.Text))
            {                
                //MessageBox.Show("车方数量不能为空");
                lbl_message.Text = "车方数量不能为空";
                txt_Cubic.Focus();
                return;
            }
            if (!ValidationRegex.VadidateData(txt_Cubic.Text))
            {
                lbl_message.Text = "车方数量只能为数字！";
                txt_Cubic.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_License.Text))
            {
                lbl_message.Text = "车牌号不能为空";
                txt_License.Focus();
                return;
            }
           
                //NewCar.PID = Convert.ToInt32(cmb_Pid.SelectedValue.ToString());
                //NewCar.Cubic = Convert.ToInt32(txt_Cubic.Text);
                //NewCar.CarNo = txt_CarNo.Text;
                //NewCar.LicensePlate = txt_License.Text;
                //NewCar.CreateDate = DateTime.Now;
                //NewCar.Type = 1;
            
            try
            {
                if (NewCar == null)
                {
                    carBLL.Save(new Car { PID = Convert.ToInt32(cmb_Pid.SelectedValue.ToString()), CarNo = txt_CarNo.Text, Type = 1, CreateDate = DateTime.Now, Cubic = Convert.ToInt32(txt_Cubic.Text), LicensePlate = txt_License.Text });
                    lbl_message.Text = "信息保存成功";
                    clearText();
                }
                else
                {
                    NewCar.PID = Convert.ToInt32(cmb_Pid.SelectedValue.ToString());
                    NewCar.Cubic = Convert.ToInt32(txt_Cubic.Text);
                    NewCar.CarNo = txt_CarNo.Text;
                    NewCar.LicensePlate = txt_License.Text;
                    NewCar.CreateDate = DateTime.Now;
                    NewCar.Type = 1;
                    carBLL.Save(NewCar);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                //carBLL.Save(NewCar);
                ////MessageBox.Show("保存成功");
                //lbl_message.Text = "信息保存成功";
                //clearText();
                //if (!flag) {
                //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                //    this.Close();
                //}
                
            }
            catch (Exception ex)
            {

                MessageBox.Show("出现错误请联系管理员。错误信息" + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clearText();
        }
        void clearText()
        {
            txt_CarNo.Text = "";
            txt_Cubic.Text = "";
            txt_License.Text = "";
            cmb_Pid.Focus();
            //cmb_Pid.SelectedIndex = 0;
        }

        private void cmb_Pid_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                txt_CarNo.Focus();
            }
        }

        private void txt_CarNo_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                txt_License.Focus();
            }
        }

        private void txt_License_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                txt_Cubic.Focus();
            }
        }

        private void txt_Cubic_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                button1.Focus();
            }
        }        
    }
}
